﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight.Navigation.History
{
    /// <summary>
    /// Represents all items in an application's history.
    /// </summary>
    public class HistoryItemCollection : ObservableCollection<HistoryItem>
    {
        /// <summary>
        /// Gets today's history items.
        /// </summary>
        /// <returns></returns>
        public HistoryItemCollection Today()
        {
            HistoryItemComparer comparer = new HistoryItemComparer();

            HistoryItemCollection value = new HistoryItemCollection();

            var items = (from item in this.Items
                         where item.Timestamp.Date == DateTime.Now.Date
                         select item).OrderByDescending(i => i.Timestamp).Distinct(comparer).ToList();

            if (items != null)
            {
                foreach (HistoryItem item in items)
                    value.Add(item);
            }

            return value;
        }

        /// <summary>
        /// Gets this week's history items.
        /// </summary>
        /// <returns></returns>
        public HistoryItemCollection ThisWeek()
        {
            HistoryItemComparer comparer = new HistoryItemComparer();

            HistoryItemCollection value = new HistoryItemCollection();

            var items = (from item in this.Items
                         where item.Timestamp.Date >= DateTime.Now.Date.Subtract(new TimeSpan(7, 0, 0,0))
                         select item).OrderByDescending(i => i.Timestamp).Distinct(comparer).ToList();

            if (items != null)
            {
                foreach (HistoryItem item in items)
                    value.Add(item);
            }

            return value;
        }

        /// <summary>
        /// Gets last week's history items.
        /// </summary>
        /// <returns></returns>
        public HistoryItemCollection LastWeek()
        {
            HistoryItemComparer comparer = new HistoryItemComparer();

            HistoryItemCollection value = new HistoryItemCollection();

            var items = (from item in this.Items
                         where item.Timestamp <= DateTime.Now.Date.Subtract(new TimeSpan(8, 0, 0, 0)) &&
                         item.Timestamp >= DateTime.Now.Date.Subtract(new TimeSpan(14, 0, 0, 0))
                         select item).OrderByDescending(i => i.Timestamp).Distinct(comparer).ToList();

            if (items != null)
            {
                foreach (HistoryItem item in items)
                    value.Add(item);
            }

            return value;
        }

        /// <summary>
        /// Gets history items from two weeks ago.
        /// </summary>
        /// <returns></returns>
        public HistoryItemCollection TwoWeeksAgo()
        {
            HistoryItemComparer comparer = new HistoryItemComparer();

            HistoryItemCollection value = new HistoryItemCollection();

            var items = (from item in this.Items
                         where item.Timestamp <= DateTime.Now.Date.Subtract(new TimeSpan(15, 0, 0, 0)) &&
                         item.Timestamp >= DateTime.Now.Date.Subtract(new TimeSpan(21, 0, 0, 0))
                         select item).OrderByDescending(i => i.Timestamp).Distinct(comparer).ToList();

            if (items != null)
            {
                foreach (HistoryItem item in items)
                    value.Add(item);
            }

            return value;
        }

        /// <summary>
        /// Get history items from more than two weeks ago.
        /// </summary>
        /// <returns></returns>
        public HistoryItemCollection MoreThanTwoWeeksAgo()
        {
            HistoryItemComparer comparer = new HistoryItemComparer();

            HistoryItemCollection value = new HistoryItemCollection();

            var items = (from item in this.Items
                         where item.Timestamp <= DateTime.Now.Date.Subtract(new TimeSpan(22, 0, 0, 0))
                         select item).OrderByDescending(i => i.Timestamp).Distinct(comparer).ToList();

            if (items != null)
            {
                foreach (HistoryItem item in items)
                    value.Add(item);
            }

            return value;
        }
    }
}
